package com.work.mdass.controller;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.work.mdass.domain.OrderApply;
import com.work.mdass.domain.Result;
import com.work.mdass.service.OrderApplyService;
import com.work.mdass.util.OrderApplyResultUtil;

@Controller
@RequestMapping("/admin/apply")
public class OrderApplyController {

	@Autowired
	private OrderApplyService orderApplyService;
	 /*
	  *  新增一条申请记录
	  */
	@PostMapping("/save")
	@ResponseBody
	public Result<OrderApply> save(@Valid OrderApply orderApply,BindingResult bindingResult ){
		
		
		if(bindingResult.hasErrors()) {
			return OrderApplyResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
		}else 
			return OrderApplyResultUtil.success(orderApplyService.addOrderApply(orderApply));
	}
	/*
	 * 获得所有的申请记录
	 */
	@GetMapping("/getAll")
	@ResponseBody
	public Map<Object,Object> getAll(){
		return orderApplyService.getAll();
	}
	/*
	 * 通过教师编号查找记录
	 */
	@PostMapping("/byTSerial")
	@ResponseBody
	public Map<Object,Object> getApplyByTSerial(@RequestParam(required = false) Integer tSerial){
		
		return orderApplyService.getOrderApplyByTS(tSerial);
	}
	
	/*
	 * 通过关键字搜索
	 */
	@GetMapping("/byKeyWord")
	@ResponseBody
	public Map<Object,Object> getByKeyWord(@RequestParam(required = false) Integer keyWord){
		return orderApplyService.getOrderApplyByKW(keyWord);
	}
	/*
	 * 通过申请编号查找记录
	 */
	@PostMapping("/bySerial")
	@ResponseBody
	public Result<OrderApply> getBySerial(@RequestParam(required = false) Integer applySerial){

		
		return OrderApplyResultUtil.success(orderApplyService.getByApplySerial(applySerial));
	}
	/*
	 * 审核
	 */
	@PostMapping("/updata")
	@ResponseBody
	public Map<Object,Object> updata(@Valid OrderApply orderApply){
		
		return orderApplyService.updata(orderApply);
	}
	
	/*
	 * 删除一条记录
	 */
	@PostMapping("/delete")
	@ResponseBody
	public Result<OrderApply> delete(@RequestParam(required=false) Integer id){
		return OrderApplyResultUtil.success(orderApplyService.deleteByid(id));
	}
}
